package servlet;

import server.HttpProtocolUtil;
import server.Request;
import server.Response;
import server.servlet.HttpServlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:
 * @author: Archie Wan
 * @time: 2020/12/28 13:15
 */
public class MyServlet02 extends HttpServlet {
    
    @Override
    public void doGet(Request request, Response response) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(new Date());
        String content = String.format("<h1>MyServlet doGet from demo2. [%s]</h1>", currentTime);
        try {
            response.output((HttpProtocolUtil.getHttpHeader200(content.getBytes().length) + content));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void doPost(Request request, Response response) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(new Date());
        String content = String.format("<h1>MyServlet doPost from demo2. [%s]</h1>", currentTime);
        try {
            response.output((HttpProtocolUtil.getHttpHeader200(content.getBytes().length) + content));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void init() throws Exception {
        System.out.println("MyServlet init from demo2");
    }
    
    @Override
    public void destroy() throws Exception {
        System.out.println("MyServlet destroy from demo2");
    }
}
